package com.ojoin.trade.common.web.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.constdef.IConstDef;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.FileUploadUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.web.dao.BaseDao;
import com.ojoin.trade.common.web.dao.TbUploadFileDao;
import com.ojoin.trade.common.web.domain.TbUploadFile;
import com.ojoin.trade.common.web.service.TbUploadFileService;

@Service
public class TbUploadFileServiceImpl extends
		BaseServiceImpl<TbUploadFile> implements
		TbUploadFileService {
	
	private static final Logger logger = LoggerFactory
			.getLogger(TbUploadFileServiceImpl.class);
	
	@Autowired
	private TbUploadFileDao dao;
	
	@Override
	protected BaseDao<TbUploadFile> getBaseDao() {
		return dao; 
	}

	public int updateStatusById(int id, String status) throws BusinessException {
		return ((TbUploadFileDao) this.getBaseDao()).updateStatusById(id, status);
	}

	public List<TbUploadFile> queryUnCommitAfter(int day) throws BusinessException {
		DateTime cdt = new DateTime();
	    DateTime oneMonthAfter=cdt.minusDays(day);
		return dao.queryUnCommitAfter(oneMonthAfter.toDate());
	}
	
	public void deleteUnCommit(TbUploadFile file) throws BusinessException {
		String cxt = FileUploadUtils.getDefaultBaseDir() + File.separator;

		if (StringUtils.isNotEmpty(file.getDownloadPath())) {
			File dicRes = new File(cxt + file.getDownloadPath());
			// remove file
			if (null != dicRes && dicRes.exists()) {
				deleteRes(new File[] { dicRes }, file);
			}
		}
	}
	
	/**
	 * 递归清理子目录
	 * @param f
	 * @param deadLine
	 */
	private void deleteRes(File[] f,TbUploadFile uFile){
		for(File curr :f){
			if(curr.isDirectory()){
//				deleteRes(curr.listFiles(),uFile);
				LogUtils.error(logger,"不允许的目录->"+curr.getName());
			}else{
				//是否达到清理时间
				 ((TbUploadFileService)AopContext.currentProxy()).updateStatusById(uFile.getId(), IConstDef.UPLOADSTATUS_REMOVED);
				 if(!curr.delete()){
					 throw new BusinessException("清理文件失败:"+uFile.getId());
				 }else{
					 LogUtils.info(logger,"成功清理超时资源 id %s 名称 %s",uFile.getId(),curr.getName());
				 }
			}
		}
	}

	@Override
	public int updateFileTypeAndStatusById(int id, String status,
			String filetype) throws BusinessException {
		return ((TbUploadFileDao) this.getBaseDao()).updateFileTypeAndStatusById(id, status, filetype);
	}

	@Override
	public List<TbUploadFile> getFileListByCondition(Integer joinuserid, String filetype, String platformtype, String startdate,
			String enddate,Pageable page) throws BusinessException {
		
		return ((TbUploadFileDao) this.getBaseDao()).getFileListByCondition(joinuserid, filetype, platformtype, startdate, enddate,page);
		
	}
}
